package com.stemaini.utility;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AdhiK' 2008
 */
public class NodeFile {
    
    private String path;
    private boolean isValid;
    private File file;
    
    private String judul,
                   tanggal,
                   sumber,
                   isi,
                   idKelas;
    private int  idSumber;
    
    public NodeFile(String path, File file) {
        this.path = path;
        this.file = file;
    }

    public File getFile() {
        return file;
    }

    public String getIdKelas() {
        return idKelas;
    }

    public boolean isIsValid() {
        return isValid;
    }

    public String getIsi() {
        return isi;
    }

    public String getJudul() {
        return judul;
    }

    public String getPath() {
        return path;
    }

    public String getSumber() {
        return sumber;
    }

    public String getTanggal() {
        return tanggal;
    }

    public int getIdSumber() {
        return idSumber;
    }

    public void setIdSumber(int idSumber) {
        this.idSumber = idSumber;
    }

    
    public boolean parseFile(){
        if(!path.isEmpty()){
            file = new File(path);
            if((!file.exists())||(!file.canRead())||(!file.isFile()))
                return false;
            else{
                try {
                    BufferedReader in = null;
                    String text;
                    in = new BufferedReader(new FileReader(file));
                    while ((text = in.readLine()) != null) {
                        String[] pecah = text.split(" ");
                        if(pecah.length!=0){
                            if(pecah[0].equalsIgnoreCase("@judul"))
                                this.judul = getContent(pecah);
                            else if(pecah[0].equalsIgnoreCase("@tanggal")){
                                String tmp = getContent(pecah);
                                String[] brk = tmp.split("-");
                                this.tanggal = brk[2].trim()+"-"+brk[1].trim()+"-"+brk[0];
                            }
                            else if(pecah[0].equalsIgnoreCase("@sumber"))
                                this.sumber = getContent(pecah);
                            else if(pecah[0].equalsIgnoreCase("@IDKelas"))
                                this.idKelas = getContent(pecah);
                            else if(pecah[0].equalsIgnoreCase("@isi")){
                                this.isi = getContent(pecah);
                                while ((text = in.readLine()) != null){
                                    this.isi += text;
                                }
                                break;
                            }
                        }
                    }
                    if(cekValiditas())
                        return true;
                    else
                        return false;
                    
                } catch (IOException ex) {
                    Logger.getLogger(NodeFile.class.getName()).log(Level.SEVERE, null, ex);
                    this.isValid = false;
                } 
            }
        }
        return false;
    }
    
    private String getContent(String[] s){
        String hasil = "";
        for(int i=1;i<s.length;i++){
            hasil += s[i]+" "; 
        }
        return hasil;
    }
    
    private boolean cekValiditas()
    {
        if(judul==null || tanggal == null || sumber==null 
                || idKelas==null || isi==null){
            this.isValid = false;
            System.out.println(this.getJudul()+"\n"+this.getTanggal()+"\n"+this.getSumber()+"\n"+
                    this.getIdKelas()+"\n"+this.getIsi());
            return false;
        }
        
        if(judul.isEmpty() || tanggal.isEmpty() 
                || sumber.isEmpty() || sumber.isEmpty() || idKelas.isEmpty()
                || isi.isEmpty()){
            this.isValid = false;
            System.out.println("Ada yg nda ada isinya");
            return false;
        }
       
        this.isValid = true;
        return true;
    }
}
